﻿using B.X.Rabbit.Domain.Autofac;
using B.X.Rabbit.Domain.商品;
using B.X.Rabbit.ErrorCode;
using B.X.Rabbit.Infrastructure;
using B.X.Rabbit.Read.Api.Application.Command.Query;
using B.X.Rabbit.Read.Api.DTO;
using MediatR;
using System.Collections.Generic;

namespace B.X.Rabbit.Read.Api.Application.CommandHandler.Query
{
    /// <summary>
    /// 查询溯源信息
    /// </summary>
    public class QueryCommandHandler : IRequestHandler<QueryCommand, ApiResult<ApiPaging<QueryDto>>>
    {
        /// <summary>
        /// 溯源信息表
        /// </summary>
        private readonly IBaseReponsitory<Traceabilitys> baseReponsitory;
        /// <summary>
        /// 商品表
        /// </summary>
        private readonly IBaseReponsitory<Good> goodReponsitory;
        /// <summary>
        /// 基础信息
        /// </summary>
        private readonly IBaseReponsitory<BaseFieId> baseFieId;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="baseReponsitory">溯源信息表</param>
        /// <param name="goodReponsitory">商品表</param>
        /// <param name="baseFieId">基础信息</param>
        public QueryCommandHandler(IBaseReponsitory<Traceabilitys> baseReponsitory, IBaseReponsitory<Good> goodReponsitory, IBaseReponsitory<BaseFieId> baseFieId)
        {
            this.baseReponsitory = baseReponsitory;
            this.goodReponsitory = goodReponsitory;
            this.baseFieId = baseFieId;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">命令</param>
        /// <param name="cancellationToken">请求</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<ApiPaging<QueryDto>>> Handle(QueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<QueryDto>> result = new ApiResult<ApiPaging<QueryDto>>();
            result.Code = ResultCode.查询成功;
            result.Msg = ResultCode.查询成功.ToString();
            var linq = from a in baseReponsitory.GetAll()
                       join b in goodReponsitory.GetAll() on a.GoodsId equals b.GoodId
                       where a.TraceabilityId == request.CurrentUserId 
                       select new QueryDto
                       {
                           TraceabilityId = a.TraceabilityId,
                           GoodsId = a.GoodsId,
                           GoodName = b.GoodName,
                           ProductionBatch = a.ProductionBatch,
                           ExpirationDate = a.ExpirationDate,
                           ProductionLicense = a.ProductionLicense,
                           ProductionEnterprise = a.ProductionEnterprise,
                           CreateOperatorName = a.CreateOperatorName,
                           CreateDate = a.CreateDate,
                           ModifyOperatorName = a.ModifyOperatorName,
                       };
            //分页
            ApiPaging<QueryDto> page = new ApiPaging<QueryDto>();
            page.TotalCount = linq.Count();
            page.ToatlPage = (int)Math.Ceiling(linq.Count() * 1.0 / request.PageSixe);
            page.list = linq.OrderByDescending(x => x.TraceabilityId)
                            .Skip(request.PageSixe * (request.PageIndex - 1))
                            .Take(request.PageSixe)
                            .ToList();
            result.Data = page;



            return Task.FromResult(result);
        }
    }
}
